using System;
using System.Linq;
using UnityEngine;
using Voodoo.Sauce.Core;
using Voodoo.Sauce.Internal.Extension;
using Voodoo.Sauce.Internal.VoodooTune;
using Voodoo.Sauce.Privacy;

namespace Voodoo.Sauce.Internal.Analytics
{
	internal static class AbTestManager
	{
		private const string TAG = "AbTestManager";

		private static bool _debugModeForced;

		private static string[] _runningAbTests;

		private static bool _useVoodooTune;

		private static VoodooSettings _voodooSettings;

		internal static void Initialize(VoodooSettings settings)
		{
			VoodooLog.LogDebug(Module.ANALYTICS, "AbTestManager", "Initializing AB Tests");
			_voodooSettings = settings;
			_useVoodooTune = _voodooSettings.UseRemoteConfig;
			if (_useVoodooTune)
			{
				return;
			}
			string[] runningABTests = _voodooSettings.GetRunningABTests();
			if (runningABTests != null && runningABTests.Length != 0)
			{
				_runningAbTests = AbTestHelper.AddControlCohortsToAbTests(runningABTests);
				DebugForcedCohort debugForcedCohort = _voodooSettings.GetDebugForcedCohort();
				if (debugForcedCohort.IsDebugCohort())
				{
					VoodooLog.LogDebug(Module.ANALYTICS, "AbTestManager", "Re-initializing cohort because debug is enabled.");
					_debugModeForced = true;
					SetPlayerCohort(debugForcedCohort.HasForcedNoCohort() ? null : debugForcedCohort.GetCohort());
				}
				else if (ShouldCreateCohort(_voodooSettings))
				{
					SetPlayerCohort(AbTestHelper.GenerateNewRandomCohort(_runningAbTests, _voodooSettings.GeUsersPercentPerCohort()));
				}
				VoodooLog.LogDebug(Module.ANALYTICS, "AbTestManager", $"AB Test Status: AB Test: {GetPlayerCohort()} - Event tracked: {PlayerIsInACohort()} - People tracked: false");
			}
		}

		private static bool ShouldCreateCohort(VoodooSettings settings)
		{
			if (!AnalyticsStorageHelper.Instance.IsFirstAppLaunch())
			{
				VoodooLog.LogDebug(Module.ANALYTICS, "AbTestManager", "Not initializing AB Test because not first time app launched");
				return false;
			}
			string locale = PrivacyUtils.GetLocale().ToLower();
			string[] array = (settings.IsCustomABTestsCountryCodesEnabled() ? settings.GetCustomABTestsCountryCodes() : new string[1] { "us" });
			if (array == null || array.All((string code) => code.ToLower() != locale))
			{
				VoodooLog.LogDebug(Module.ANALYTICS, "AbTestManager", "Not initializing AB Test with " + locale);
				return false;
			}
			VoodooLog.LogDebug(Module.ANALYTICS, "AbTestManager", $"Initializing AB Test because first time app launched with {Application.platform} - {locale}");
			return true;
		}

		internal static bool PlayerIsInACohort()
		{
			return GetPlayerCohort() != null;
		}

		internal static string GetPlayerCohort()
		{
			if (_useVoodooTune)
			{
				return VoodooTuneManager.GetMainAbTestCohortName();
			}
			string savedPlayerCohort = AbTestHelper.GetSavedPlayerCohort();
			if (!string.IsNullOrEmpty(savedPlayerCohort) && _runningAbTests != null)
			{
				if (_runningAbTests.Contains(savedPlayerCohort))
				{
					return savedPlayerCohort;
				}
				SetPlayerCohort(null);
			}
			return null;
		}

		internal static string[] GetPlayerCohorts()
		{
			if (_useVoodooTune)
			{
				return VoodooTuneManager.GetAbTestCohortUuidsAsList().ToArray();
			}
			string playerCohort = GetPlayerCohort();
			if (playerCohort == null)
			{
				return new string[0];
			}
			return new string[1] { playerCohort };
		}

		internal static int GetPlayerCohortIndex()
		{
			string savedPlayerCohort = AbTestHelper.GetSavedPlayerCohort();
			if (savedPlayerCohort != null && _runningAbTests != null)
			{
				return Array.IndexOf(_runningAbTests, savedPlayerCohort);
			}
			return -1;
		}

		internal static void SetPlayerCohort(string cohort)
		{
			if (!_useVoodooTune)
			{
				if (_voodooSettings == null)
				{
					VoodooLog.LogError(Module.ANALYTICS, "AbTestManager", "Do not set player cohort before AbTestManager initialization");
					return;
				}
				AbTestHelper.SavePlayerCohort(cohort);
				TrackAbTestAssignment(_voodooSettings.LegacyAbTestName, cohort);
			}
		}

		private static void TrackAbTestAssignment(string abTest, string cohort)
		{
			if (string.IsNullOrEmpty(abTest) || string.IsNullOrEmpty(cohort))
			{
				VoodooLog.LogError(Module.ANALYTICS, "AbTestManager", "TrackAbTestAssignment: parameters should not be null or empty");
			}
		}

		private static void TrackAbTestExit(string abTest, string cohort)
		{
			if (string.IsNullOrEmpty(abTest) || string.IsNullOrEmpty(cohort))
			{
				VoodooLog.LogError(Module.ANALYTICS, "AbTestManager", "TrackAbTestExit: parameters should not be null or empty");
			}
		}

		internal static string[] GetAbTests()
		{
			return _runningAbTests;
		}

		internal static bool IsDebugModeForced()
		{
			return _debugModeForced;
		}
	}
}
